package luit.mmt02.adapter;

import java.util.List;

import javax.mail.Message;

import luit.mmt02.R;
import luit.mmt02.activity.readmailActivity;
import luit.mmt02.dataAccess.db_email;
import luit.mmt02.doituong.OneMail;
import luit.mmt02.doituong.showToast;
import luit.mmt02.xuly.ConvertMessageToList;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

// class la 1 adapter bind du lieu tu list Onemail vao rowlayout hien thi len list
public class myadapter extends ArrayAdapter<OneMail>
{
	//
//	static final String ATTACH = "attach";
//	static final String VOICE = "voice";
//	static final String ENCRYPT = "encrypt";
//	//
//	final static String KEY_SUB = "SUB";
//	final static String KEY_FROM = "FROM";
//	final static String KEY_TO = "TO";
//	final static String KEY_TIME = "TIME";
//	final static String KEY_BODY = "BODY";
//	final static String KEY_FAVORITE = "FAVORITE";
	
	final static String KEY_STT = "_stt";
	final static String KEY_DB = "_key_db";
	final static String KEY_BUNDLE = "BUNDLE";
	final static String KEY_MESS = "message";
	final static String KEY_USERNAME = "username";
	
	
	List<OneMail>list;
	Activity context;
	showToast st;
	Message[]mes;
	String db_name, username, tag = "myadapter";
	int position_mess;
	OneMail omG;
	ConvertMessageToList cv;
	public myadapter(Activity context, List<OneMail>list) 
	{
		super(context, R.layout.row_duyetmail,list);
		this.context = context;
		this.list = list;
		st = new showToast(context);
		// TODO Auto-generated constructor stub
	}
	
	public myadapter(Activity context, List<OneMail>list, Message[]mes, String db_name, String username) 
	{
		super(context, R.layout.row_duyetmail,list);
		Log.v(tag, "contructor");
		this.context = context;
		this.list = list;
		this.mes = mes;
		this.db_name = db_name;
		this.username = username;
		st = new showToast(context);
		cv = new ConvertMessageToList();
		// TODO Auto-generated constructor stub
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		// TODO Auto-generated method stub
		View v = null;
		if(convertView == null)
		{
			LayoutInflater li = context.getLayoutInflater();
			v = li.inflate(R.layout.row_duyetmail, null);
		}
		else
		{
			v = convertView;
		}
		final OneMail om = list.get(position);
		
		TextView txtStt = (TextView)v.findViewById(R.id.txtSTT);
		TextView txtFrom = (TextView)v.findViewById(R.id.txtNameFrom);
		TextView txtTime = (TextView)v.findViewById(R.id.txtTime);
		TextView txtSub= (TextView)v.findViewById(R.id.txtSub);
		TextView txtbodySort= (TextView)v.findViewById(R.id.txtbodySort);
//		ImageView img_att = (ImageView)v.findViewById(R.id.image_duyetmail_attach);
//		ImageView img_voice = (ImageView)v.findViewById(R.id.image_duyetmail_voice);
////		ImageView img_encrypt = (ImageView)v.findViewById(R.id.image_duyetmail_mahoa);
//		ImageButton img_enc = (ImageButton)v.findViewById(R.id.image_duyetmail_mahoa2);
//		CheckBox cb_favorite = (CheckBox)v.findViewById(R.id.cb_duyetmail_favorite);
//		CheckBox cb_check = (CheckBox)v.findViewById(R.id.cb_duyetmail_check); // check box ben trai
//		cb_check.setOnClickListener(new OnClickListener() {
//			
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				CheckBox cb_check_temp = (CheckBox)v.findViewById(R.id.cb_duyetmail_check);
//				if(cb_check_temp.isChecked())
//				{
//					om.setCheck(true);
//					st.showmess("check left");
//				}
//				else
//				{
//					om.setCheck(false);
//					st.showmess("un check left");
//				}
//				
//			}
//		});
//		cb_favorite.setOnClickListener(new OnClickListener() {
//			
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				CheckBox cb_temp = (CheckBox)v.findViewById(R.id.cb_duyetmail_favorite);
////				ConvertMessageToList cv = new ConvertMessageToList();
//				
//				position_mess = mes.length - om.getId();  //list dem tu tren xuong, con mess dem tu duoi len ...
//				Log.v("vi tri stard", "la : " + position_mess);
//				db_email mydb = new db_email(context, db_name);
//				if(cb_temp.isChecked())
//				{
//					om.setFlagFavorite(true);
//					st.showmess("checked");
//					if(mes.length > 0)
//					{
//						cv.set_favorite_true(mes, position_mess);
////						//update database
//						mydb.open();
//						mydb.update_row_by_id(om.getId(), true);
//						mydb.close();
//					}else
//					{
//						st.showmess("kiem tra lai ket noi mang...");
//					}
//				}
//				else
//				{
//					om.setFlagFavorite(false);
//					cv.set_favorite_false(mes, position_mess);
//					mydb.open();
//					mydb.update_row_by_id(om.getId(), false);
//					mydb.close();
//					st.showmess("un checked");
//				}
//			}
//		});
		//gan vao
		
		txtStt.setText(String.valueOf(om.getId()));
		txtFrom.setText(list.get(position).getFrom());
		txtTime.setText(list.get(position).getDateSend());
		txtSub.setText(list.get(position).getSub());
		txtbodySort.setText(list.get(position).getBody());
//		if(om.isFlagAttach())
//		{
//			img_att.setVisibility(ImageView.VISIBLE);
//		}
//		else
//		{
//			img_att.setVisibility(ImageView.GONE);
//		}
//		
//		if(om.isFlagEncrypt())
//		{
//			img_enc.setVisibility(ImageView.VISIBLE);
//			Log.v(tag, "hien thi is true");
//		}
//		else
//		{
//			img_enc.setVisibility(ImageView.GONE);
//			Log.v(tag, "hien thi is false");
//		}
////		if(om.isFlagVoice())
////		{
////			img_voice.setVisibility(ImageView.VISIBLE);
////		}
////		else
////		{
////			img_encrypt.setVisibility(ImageView.GONE);
////		}
//		cb_favorite.setChecked(om.isFlagFavorite());
//		cb_check.setChecked(om.isCheck());
		v.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//bien mail chua doc thanh doc roi
//				if(!om.isFlagRead()) // neu thu chua doc
//				{
//					position_mess = mes.length - om.getId();
//					cv.unread_to_read(mes, position_mess);
//					Log.v(tag, "unread to read mess thu :  " + position_mess);
//					
//					//update csdl
//				}
				Intent I_readmail = new Intent(context, readmailActivity.class);
				I_readmail.putExtra(KEY_STT, om.getId());
				I_readmail.putExtra(KEY_DB, db_name);
				I_readmail.putExtra(KEY_USERNAME,username);
				context.startActivity(I_readmail);
				Log.i("myadapter", "item click");
			}
		});
		return v;
	}
}
